package city.spring.controller;

import city.spring.domain.entity.RetreatSettleaccountsEntity;
import city.spring.service.RetreatSettleaccountsService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：退住结算
 *
 * @author 谢金婷
 * @date 2020-04-17 10:54:40
 */
@RestController
@RequestMapping("/retreat_settleaccounts")
public class RetreatSettleaccountsController extends ApiController {
    private final RetreatSettleaccountsService retreatSettleaccountsService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<RetreatSettleaccountsEntity, ?>> entityOrderFields;

    public RetreatSettleaccountsController(RetreatSettleaccountsService retreatSettleaccountsService) {
        this.retreatSettleaccountsService = retreatSettleaccountsService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(RetreatSettleaccountsEntity::getId);
        entityOrderFields.add(RetreatSettleaccountsEntity::getOldmanId);
        entityOrderFields.add(RetreatSettleaccountsEntity::getBedId);
        entityOrderFields.add(RetreatSettleaccountsEntity::getNursingLevel);
        entityOrderFields.add(RetreatSettleaccountsEntity::getRetreatdate);
        entityOrderFields.add(RetreatSettleaccountsEntity::getDate);
        entityOrderFields.add(RetreatSettleaccountsEntity::getCostCategory);
        entityOrderFields.add(RetreatSettleaccountsEntity::getTotal);
        entityOrderFields.add(RetreatSettleaccountsEntity::getExplanation);
        entityOrderFields.add(RetreatSettleaccountsEntity::getClassification);
        entityOrderFields.add(RetreatSettleaccountsEntity::getSum);
        entityOrderFields.add(RetreatSettleaccountsEntity::getTotalRefund);
        entityOrderFields.add(RetreatSettleaccountsEntity::getTotalCheckinFee);
        entityOrderFields.add(RetreatSettleaccountsEntity::getOtherIncome);
        entityOrderFields.add(RetreatSettleaccountsEntity::getTotalArrears);
        entityOrderFields.add(RetreatSettleaccountsEntity::getTotalDeposit);
        entityOrderFields.add(RetreatSettleaccountsEntity::getTotalStored);
        entityOrderFields.add(RetreatSettleaccountsEntity::getPayable);
        entityOrderFields.add(RetreatSettleaccountsEntity::getRefundable);
        entityOrderFields.add(RetreatSettleaccountsEntity::getRetreatdate);
        entityOrderFields.add(RetreatSettleaccountsEntity::getStatus);
    }

    /**
     * 获取全部的 <strong>退住结算</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, RetreatSettleaccountsEntity entity) {
        LambdaQueryChainWrapper<RetreatSettleaccountsEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>退住结算</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, RetreatSettleaccountsEntity entity) {
        LambdaQueryChainWrapper<RetreatSettleaccountsEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<RetreatSettleaccountsEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>退住结算</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(retreatSettleaccountsService.getById(id));
    }

    /**
     * 添加一个 <strong>退住结算</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody RetreatSettleaccountsEntity entity) {
        retreatSettleaccountsService.saveRetreatSettleaccounts(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>退住结算</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody RetreatSettleaccountsEntity entity) {
        entity.setId(id);
        retreatSettleaccountsService.updateRetreatSettleaccounts(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>退住结算</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        retreatSettleaccountsService.deleteRetreatSettleaccounts(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>退住结算</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        retreatSettleaccountsService.deleteRetreatSettleaccounts(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<RetreatSettleaccountsEntity> buildLambdaQuery(RetreatSettleaccountsEntity entity) {
        LambdaQueryChainWrapper<RetreatSettleaccountsEntity> lambdaQuery = retreatSettleaccountsService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), RetreatSettleaccountsEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), RetreatSettleaccountsEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBedId()), RetreatSettleaccountsEntity::getBedId, entity.getBedId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getNursingLevel()), RetreatSettleaccountsEntity::getNursingLevel, entity
                .getNursingLevel());
        lambdaQuery.like(entity.getRetreatdate() != null, RetreatSettleaccountsEntity::getRetreatdate, entity.getRetreatdate());
        lambdaQuery.like(entity.getDate() != null, RetreatSettleaccountsEntity::getDate, entity.getDate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCostCategory()), RetreatSettleaccountsEntity::getCostCategory, entity
                .getCostCategory());
        lambdaQuery.like(entity.getTotal() != null, RetreatSettleaccountsEntity::getTotal, entity.getTotal());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getExplanation()), RetreatSettleaccountsEntity::getExplanation, entity
                .getExplanation());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getClassification()), RetreatSettleaccountsEntity::getClassification, entity
                .getClassification());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSum()), RetreatSettleaccountsEntity::getSum, entity.getSum());
        lambdaQuery.like(entity.getTotalRefund() != null, RetreatSettleaccountsEntity::getTotalRefund, entity.getTotalRefund());
        lambdaQuery.like(entity.getTotalCheckinFee() != null, RetreatSettleaccountsEntity::getTotalCheckinFee, entity.getTotalCheckinFee());
        lambdaQuery.like(entity.getOtherIncome() != null, RetreatSettleaccountsEntity::getOtherIncome, entity.getOtherIncome());
        lambdaQuery.like(entity.getTotalArrears() != null, RetreatSettleaccountsEntity::getTotalArrears, entity.getTotalArrears());
        lambdaQuery.like(entity.getTotalDeposit() != null, RetreatSettleaccountsEntity::getTotalDeposit, entity.getTotalDeposit());
        lambdaQuery.like(entity.getTotalStored() != null, RetreatSettleaccountsEntity::getTotalStored, entity.getTotalStored());
        lambdaQuery.like(entity.getPayable() != null, RetreatSettleaccountsEntity::getPayable, entity.getPayable());
        lambdaQuery.like(entity.getRefundable() != null, RetreatSettleaccountsEntity::getRefundable, entity.getRefundable());
        return lambdaQuery;
    }

}
